package com.dmcb.trade.business.mappers;

import com.dmcb.trade.business.entities.Article;
import com.dmcb.trade.business.entities.MonitorArticle;
import com.dmcb.trade.business.entities.MonitorWechat;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.StatementType;

/**
 * 文章监测
 * Created by long on 2016/3/24.
 */
public interface ArticleMonitorMapper {

    /**
     * 保存监控微信号
     *
     * @param mw
     */
    @SelectKey(before = false, keyProperty = "mw.id", statementType = StatementType.STATEMENT, statement = "SELECT LAST_INSERT_ID() AS id", resultType = Integer.class)
    @Insert(value = "INSERT INTO t_monitor_wechat (userId,wechatNo,wechatName,priority,createTime,lastModTime) VALUES (#{mw.userId}, #{mw.wechatNo}, #{mw.wechatName}, 8, NOW(), NOW())")
    int insertWechat(@Param("mw") MonitorWechat mw);

    /**
     * 保存监控全文内容
     *
     * @param ma 监控文章
     */
    @SelectKey(before = false, keyProperty = "ma.id", statementType = StatementType.STATEMENT, statement = "SELECT LAST_INSERT_ID() AS id", resultType = Integer.class)
    @Insert(value = "INSERT INTO t_monitor_article (tmp_user,title,content,uri) VALUES (#{ma.tmp_user}, #{ma.title}, #{ma.content}, #{ma.uri})")
    int insertContent(@Param("ma") MonitorArticle ma);

    /**
     * 保存监控全文内容
     *
     * @param a 文章
     */
    @SelectKey(before = false, keyProperty = "a.id", statementType = StatementType.STATEMENT, statement = "SELECT LAST_INSERT_ID() AS id", resultType = Integer.class)
    @Insert(value = "INSERT INTO t_monitor_article (tmp_user,title,content,uri,cover_img_uri,postTime,author,createTime) VALUES (#{a.authorId}, #{a.title}, #{a.body}, #{a.link}, #{a.coverPath}, #{a.postTime},#{a.authorName},NOW())")
    int insertMonitorArticle(@Param("a") Article a);

    /**
     * 查询监控
     *
     * @param userId 用户ID
     */
    @Select(value = "select id, userId, finish from t_monitor_wechat where userId = #{0} limit 1")
    MonitorWechat queryByUserId(int userId);

    /**
     * 查询文章
     */
    @Select(value = "select count(*) from t_monitor_article where tmp_user = #{0} and title = #{1} limit 1")
    int countUserTitle(int tmp_user, String title);

    @Select(value = "SELECT id, title FROM t_monitor_article WHERE tmp_user = #{0} AND title = #{1} LIMIT 1")
    Article queryByUserTitle(int tmp_user, String title);

    @Update(value = "update t_monitor_wechat set wechatNo= #{0},wechatName = #{1} where id =#{2}")
    void  updateNoAName(String weChatNo, String weChatName, int id);

    @Select(value = "select id , wechatNo,wechatName ,userId from t_monitor_wechat where id = #{1} limit 1")
    MonitorWechat queryById(int id);

    @Select(value = "select id , wechatNo,wechatName ,userId from t_monitor_wechat where wechatNo = #{1} limit 1")
    MonitorWechat queryByWeChatNo(String wechatNo);

}
